package com.llsydn.handler.handler;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.llsydn.hello.R;

/**
 * @author lilinshen
 * @title Hander使用的小DEMO
 * @description 请填写相关描述
 * @date 2018/9/27 10:24
 */
public class HandlerDemoActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int WATH_INCREASE = 1;
    private static final int WATH_DECREASE = 2;
    private TextView tv_demo_number;
    private Button btn_demo_increase;
    private Button btn_demo_decrease;
    private Button btn_demo_pause;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_demo);

        tv_demo_number = findViewById(R.id.tv_demo_number);
        btn_demo_increase = findViewById(R.id.btn_demo_increase);
        btn_demo_decrease = findViewById(R.id.btn_demo_decrease);
        btn_demo_pause = findViewById(R.id.btn_demo_pause);

        btn_demo_increase.setOnClickListener(this);
        btn_demo_decrease.setOnClickListener(this);
        btn_demo_pause.setOnClickListener(this);
    }

    //创建handler对象
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //得到当前显示的数值
            int number = Integer.parseInt(tv_demo_number.getText().toString());
            switch (msg.what) {
                case WATH_INCREASE:
                    if (number >= 20) {
                        //设置暂停不能操作
                        btn_demo_pause.setEnabled(false);
                        Toast.makeText(HandlerDemoActivity.this, "已经达到最大值", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number++;
                    tv_demo_number.setText(String.valueOf(number));
                    //发送增加的延迟消息
                    handler.sendEmptyMessageDelayed(WATH_INCREASE, 1000);
                    break;
                case WATH_DECREASE:
                    if (number <= 1) {
                        //设置暂停不能操作
                        btn_demo_pause.setEnabled(false);
                        Toast.makeText(HandlerDemoActivity.this, "已经达到最小值", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    number--;
                    tv_demo_number.setText(String.valueOf(number));
                    //发减少的延迟消息
                    handler.sendEmptyMessageDelayed(WATH_DECREASE, 1000);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        if (v == btn_demo_increase) {
            btn_demo_increase.setEnabled(false);
            btn_demo_decrease.setEnabled(true);
            btn_demo_pause.setEnabled(true);
            //停止减少的消息
            handler.removeMessages(WATH_DECREASE);

            //发消息（增加）
            handler.sendEmptyMessage(WATH_INCREASE);
        } else if (v == btn_demo_decrease) {
            btn_demo_increase.setEnabled(true);
            btn_demo_decrease.setEnabled(false);
            btn_demo_pause.setEnabled(true);
            //停止增加的消息
            handler.removeMessages(WATH_INCREASE);

            //发消息（减少）
            handler.sendEmptyMessage(WATH_DECREASE);
        } else if (v == btn_demo_pause) {
            btn_demo_pause.setEnabled(false);
            btn_demo_increase.setEnabled(true);
            btn_demo_decrease.setEnabled(true);
            //停止减少的消息
            handler.removeMessages(WATH_DECREASE);
            //停止增加的消息
            handler.removeMessages(WATH_INCREASE);
        }
    }
}
